package net.seabook.games.tetris.model;


public class IBlock extends Piece {
	
	public IBlock(int x, int y) {
		for (int i = 0; i < 4; i++) {
			Tile block = new Tile(x + i*Tile.WIDTH, y);
			getBlocks().add(block);
		}
	}
	
	@Override
	public void rotate() {
		if (getrDirection().equals(RotateDirection.LEFT)) {
			if (internalTilesPosChange(1)) setrDirection(RotateDirection.UP);
		} else if (getrDirection().equals(RotateDirection.UP)){
			if (internalTilesPosChange(-1)) setrDirection(RotateDirection.LEFT);
		}
	}
	
	public boolean internalTilesPosChange(int i) {
		Tile tile1 = this.getBlocks().get(0);
		int x1 = tile1.getX() + i * Tile.WIDTH;
		int y1 = tile1.getY() - i * Tile.HEIGHT;
		Tile tile2 = this.getBlocks().get(1);
		int x2 = tile2.getX();
		int y2 = tile2.getY();
		Tile tile3 = this.getBlocks().get(2);
		int x3 = tile3.getX() - i * Tile.WIDTH;
		int y3 = tile3.getY() + i * Tile.HEIGHT;
		Tile tile4 = this.getBlocks().get(3);
		int x4 = tile4.getX() - i * 2 * Tile.WIDTH;
		int y4 = tile4.getY() + i * 2 * Tile.HEIGHT;
		
		if (isRotateOutofBound(x1) || isRotateOutofBound(x2) || isRotateOutofBound(x3) || isRotateOutofBound(x4)) {
			return false;
		}
		
		tile1.setTilePosition(x1, y1);
		tile2.setTilePosition(x2, y2);
		tile3.setTilePosition(x3, y3);
		tile4.setTilePosition(x4, y4);
		
		return true;
		
	}

}
